package com.example.prison.config;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
public class ApiUtil {
//
//    /**
//     * 获取设备在线、离线
//     */
//    public static String getDeviceStatus(String SN) {
//        String heatTime = "";
//        String status = "离线";
//        String time = timeProcess(-2);
//        Set<String> list = new HashSet<>();
//        list = AccPushProccesor.heatMap.keySet();
//        if(ObjectUtils.isEmpty(list)) {
//            return status;
//        }
//        if(ObjectUtils.isNotEmpty(AccPushProccesor.heatMap.get(SN))) {
//            heatTime = AccPushProccesor.heatMap.get(SN);
//            if (heatTime.compareTo(time) > 0) {
//                status = "在线";
//            } else {
//                status = "离线";
//            }
//        }
//        return status;
//    }

    /**
     *产生随机8位字符串(大+小字符+数字)
     */
    public static String randomStr(){
        StringBuilder str = new StringBuilder();
        //随机数生成函数
        Random random = new Random();
        //随机字符串生成
        for (int i = 0; i < 8; i++) {
            //0、1、2从中产生一个随机数，进行随机生成小写字母、大写字母、数字。
            //int ranNum = random.nextInt(3);

            switch (random.nextInt(3)){
                case 0:
                    //生成小写字母
                    char ranLowLetter = (char) ((random.nextInt(26) + 97));
                    str.append(ranLowLetter);
                    break;
                case 1:
                    //生成大写字母
                    char ranUpLetter = (char) ((random.nextInt(26) + 65));
                    str.append(ranUpLetter);
                    break;
                case 2:
                    //生成数字
                    int ranNumOut = random.nextInt(10);
                    str.append(ranNumOut);
                    break;
            }
        }
        return str.toString();
    }

    /**
     * 将将大写字母转换为小写字母，并在每个大写字母前插入”_“
     */
    public static String lowerString(String str) {
        char[] charArray = str.toCharArray(); // 将字符串转换为字符数组
        // 创建一个空的StringBuilder对象
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] >= 'A' && charArray[i] <= 'Z') { // 判断是否为大写字母
                sb.append("_");// 在每个大写字母前插入”_“
                charArray[i] += 32; // 将大写字母转换为小写字母，ASCII码中大写字母和小写字母的差为32
            }
            // 追加到StringBuilder对象中
            sb.append(charArray[i]);
        }
        String lowerCaseStr = sb.toString(); // 将字符数组转换为字符串
        return lowerCaseStr;
    }

    /**
     * 将当前时间减去time分钟并返回
     */
    public static String getRealTime() {
        Calendar rightNow = Calendar.getInstance();
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去time分钟并返回
     */
    public static String timeProcess(Integer time) {
        Calendar rightNow = Calendar.getInstance();
        //rightNow.add(Calendar.MINUTE, -10); //减10分钟
        //rightNow.add(Calendar.HOUR,1);//加一个小时
        //rightNow.add(Calendar.DAY_OF_MONTH,1);//加一天
        //rightNow.add(Calendar.MONTH,1);//加一个月
        rightNow.add(Calendar.MINUTE,time);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去day天并返回
     */
    public static String dayProcess(Integer day) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DAY_OF_MONTH,day);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去hour小时并返回
     */
    public static String hourProcess(Integer hour) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.HOUR,hour);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(dt1);
    }
    /**
     * 将当前时间减去min分钟并返回
     */
    public static String minProcess(Integer min) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.MINUTE,min);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(dt1);
    }

}
